package org.epanetgrid.otimizacao.log;

import java.io.Serializable;

import org.apache.log4j.Appender;
import org.apache.log4j.Category;
import org.apache.log4j.Logger;

/**
 * 
 * @author Cícero Alan Leite Cruz
 *
 */
public class AGLogger implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1895386473218662132L;

	//niveis
	public static final int INFO = 2<<0;
	public static final int DEBUG = 2<<1;
	public static final int WARN = 2<<2;
	
	private int somaOtim;
	
	private static AGLogger logger;
	
	private Category category;
	
	/**
	 * 
	 */
	private AGLogger() {
		somaOtim = 0;
		category = Logger.getLogger(AGLogger.class.getName());
	}
	
	/**
	 * TODO documentar
	 * @return
	 */
	public static AGLogger getInstance() {
		if(logger == null) {
			logger = new AGLogger();
		}
		return logger;
	}
	
	/**
	 * TODO documentar
	 * @param modulo
	 */
	public void addOption(int nivel) {
		somaOtim |= nivel;
	}
	
	/**
	 * Retorna se a opcao esta selecionada
	 * @param op
	 * @return
	 */
	public boolean isSelected(int nivel) {
		return (nivel & somaOtim) == nivel;
	}
	
	public void info(Object obj) {
		if( isSelected(INFO) ) {
			category.info(obj);
		}
	}
	
	public void warn(Object obj) {
		if( isSelected(WARN) ) {
			category.warn(obj);
		}
	}
	
	public void debug(Object obj) {
		if( isSelected(DEBUG) ) {
			category.debug(obj);
		}
	}
	
	/**
	 * Adiciona um novo appender ao log
	 * @param appender Appender a ser adicionado
	 */
	public void addAppender(Appender appender) {
		category.addAppender(appender);
	}
	
	/**
	 * Remove um dos appenders
	 * @param appender Appender a ser removido
	 */
	public void removeAppender(Appender appender) {
		category.removeAppender(appender);
	}
	
}
